/*!
@file
@author		Xbgzs byz
@date		02/2011
@module
*/
/*
This file is part of MyGUI.

MyGUI is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MyGUI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with MyGUI.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "MyGUI_Precompiled.h"
#include "Byz_ControllerTextureAnima.h"
#include "MyGUI_Gui.h"
#include "MyGUI_Widget.h"
#include "MyGUI_StaticImage.h"

namespace MyGUI
{
	ControllerTextureAnima::ControllerTextureAnima()
		:mInterval(0),
		mLoop(false)
	{
	}

	ControllerTextureAnima::ControllerTextureAnima(std::vector<std::string*> _texture, int _interval, bool _bool)
		:mInterval(float(_interval) / 1000.0f),
		mVectorTexture(_texture),
		mLoop(_bool)
	{
	}

	void ControllerTextureAnima::prepareItem(Widget* _widget)
	{
		mitTexture = mVectorTexture.begin();

		eventPreAction(_widget);
	}

	bool ControllerTextureAnima::addTime(Widget* _widget, float _time)
	{
		static float lasttime = 0.0f;
		lasttime += _time;
		
		if ( lasttime >= mInterval )
		{
			lasttime = 0.0f;
			std::vector<std::string*>::iterator itEnd = mVectorTexture.end();

			if (mitTexture != mVectorTexture.end())
			{
				std::string* _texture = *mitTexture;
				MyGUI::StaticImage* widget = (MyGUI::StaticImage*)(_widget);
				widget->setImageTexture(*_texture);

				mitTexture++;

				eventUpdateAction(_widget);

				return true;
			}
			else
			{
				if (mLoop)
				{
					mitTexture = mVectorTexture.begin();
					std::string* _texture = *mitTexture;
					MyGUI::StaticImage* widget = (MyGUI::StaticImage*)(_widget);
					widget->setImageTexture(*_texture);

					eventUpdateAction(_widget);

					return true;
				}
			}

		}
		else
		{
			return true;
		}

		eventPostAction(_widget);

		return false;
	}
}